package com.linxz.ui.widgets.images;

import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.linxz.ui.GuiHelper;
import com.linxz.ui.R;

import java.util.List;
/**
 * @author Linxz
 * 创建日期：2019年10月19日 23:21
 * version：v4.5.4
 * 描述：
 */
public class ImageUpLoadAdapter extends BaseQuickAdapter<ImageUploadBean, BaseViewHolder> {

    private OnImageItemClickListener listener;

    public void setOnImageItemClickListener(OnImageItemClickListener listener) {
        this.listener = listener;
    }

    public ImageUpLoadAdapter(List<ImageUploadBean> data) {
        super(R.layout.ui_item_image_upload_layout,data);
    }

    @Override
    protected void convert(final BaseViewHolder helper, final ImageUploadBean item) {
        ImageView imageView = helper.getView(R.id.image);
        ImageView imgDelete = helper.getView(R.id.imgDelete);
        if (item.isPickerBean()) {
            imageView.setImageResource(R.drawable.ui_image_add);
            imgDelete.setVisibility(View.INVISIBLE);
        } else {
            GuiHelper.loadImage(mContext, item.getPath(), imageView);
            imgDelete.setVisibility(View.VISIBLE);
        }

        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    if (item.isPickerBean()) {
                       listener.onAdd();
                    }else{
                        listener.onPicker(item,helper.getAdapterPosition());
                    }
                }
            }
        });

        imgDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener!=null){
                    listener.onDelete(item,helper.getAdapterPosition());
                }
            }
        });
    }

    interface OnImageItemClickListener {
        void onPicker(ImageUploadBean imageUploadBean, int position);

        void onDelete(ImageUploadBean imageUploadBean, int position);

        void onAdd();
    }
}
